package com.openbravo.pos.panels;

import java.util.List;

import javax.swing.JPanel;
import javax.swing.BoxLayout;

import com.openbravo.bean.BusinessPartner;
import com.openbravo.bean.erp.OperationCustomer;
import com.openbravo.bean.erp.OperationCustomerCategory;
import com.openbravo.data.basic.BasicException;
import com.openbravo.data.loader.query.QBFParameter;
import com.openbravo.data.loader.query.QBFParameters;


import com.openbravo.pos.base.AppLocal;
import e.odbo.data.dao.I_DataLogic;
import cn.ekuma.data.ui.swing.dnd.AbstractJBeanDropPanel;
import cn.ekuma.epos.db.table.erp.I_OperationCustomer;

public class JDropCustomerByOperationCategoryPanel extends JPanel {

	private JPanel panel;
	I_DataLogic dlSales ;
	QBFParameters delQBFParameters;
	QBFParameter categoryParameter=new QBFParameter(I_OperationCustomer.OPERATIONCATEGORIE);
	QBFParameter customerParameter=new QBFParameter(I_OperationCustomer.CUSTOMER);
	/**
	 * Create the panel.
	 */
	public JDropCustomerByOperationCategoryPanel(final I_DataLogic dlSales2) {
		initComponents();
		this.dlSales=dlSales2;
		delQBFParameters=new QBFParameters();
		delQBFParameters.and(categoryParameter).and(customerParameter);
		add(new AbstractJBeanDropPanel<OperationCustomerCategory,BusinessPartner>(){
			@Override
			public void doDropInsert(List<BusinessPartner> beans) {
				OperationCustomer oCustomer;
				
				for(BusinessPartner p:beans){			
					oCustomer=new OperationCustomer();
					oCustomer.setCategoryId(bean.getID());
					oCustomer.setCustomerId(p.getId());
					try {
					   dlSales.insert(oCustomer);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			}
			public void doDropDelete(List<BusinessPartner> dropList) {
				categoryParameter.equal(bean.getID());
				for(BusinessPartner p:dropList){			
					customerParameter.equal(p.getId());
					try {
					   dlSales.deleteByQBFParameters(OperationCustomer.class, delQBFParameters);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			} 
			
			@Override
			public String readerBean(OperationCustomerCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.operationCustomerCategory");
			}
			
		});
		add(new AbstractJBeanDropPanel<OperationCustomerCategory,BusinessPartner>(){
			@Override
			public void doDropInsert(List<BusinessPartner> beans) {
				OperationCustomer oCustomer;
				
				for(BusinessPartner p:beans){			
					oCustomer=new OperationCustomer();
					oCustomer.setCategoryId(bean.getID());
					oCustomer.setCustomerId(p.getId());
					try {
					   dlSales.insert(oCustomer);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			}
			
			public void doDropDelete(List<BusinessPartner> dropList) {
				categoryParameter.equal(bean.getID());
				for(BusinessPartner p:dropList){			
					customerParameter.equal(p.getId());
					try {
					   dlSales.deleteByQBFParameters(OperationCustomer.class, delQBFParameters);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			}

			@Override
			public String readerBean(OperationCustomerCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.operationCustomerCategory");
			}
			
		});
		add(new AbstractJBeanDropPanel<OperationCustomerCategory,BusinessPartner>(){
			@Override
			public void doDropInsert(List<BusinessPartner> beans) {
				OperationCustomer oCustomer;
				
				for(BusinessPartner p:beans){			
					oCustomer=new OperationCustomer();
					oCustomer.setCategoryId(bean.getID());
					oCustomer.setCustomerId(p.getId());
					try {
					   dlSales.insert(oCustomer);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			}
			
			public void doDropDelete(List<BusinessPartner> dropList) {
				categoryParameter.equal(bean.getID());
				for(BusinessPartner p:dropList){			
					customerParameter.equal(p.getId());
					try {
					   dlSales.deleteByQBFParameters(OperationCustomer.class, delQBFParameters);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			}

			@Override
			public String readerBean(OperationCustomerCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.operationCustomerCategory");
			}
			
		});
		add(new AbstractJBeanDropPanel<OperationCustomerCategory,BusinessPartner>(){
			@Override
			public void doDropInsert(List<BusinessPartner> beans) {
				OperationCustomer oCustomer;
				
				for(BusinessPartner p:beans){			
					oCustomer=new OperationCustomer();
					oCustomer.setCategoryId(bean.getID());
					oCustomer.setCustomerId(p.getId());
					try {
					   dlSales.insert(oCustomer);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			}

			public void doDropDelete(List<BusinessPartner> dropList) {
				categoryParameter.equal(bean.getID());
				for(BusinessPartner p:dropList){			
					customerParameter.equal(p.getId());
					try {
					   dlSales.deleteByQBFParameters(OperationCustomer.class, delQBFParameters);
					} catch (BasicException e) {
						e.printStackTrace();
					}
				  }
			}
			
			@Override
			public String readerBean(OperationCustomerCategory obj) {
				return obj.getName();
			}

			@Override
			public String getDropTypeTitle() {
				return AppLocal.getIntString("label.operationCustomerCategory");
			}
			
		});
	}
	private void initComponents() {
		setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS));
	}
	
	public static String getTabName(){
		return AppLocal.getIntString("label.operationCustomerCategory");
	}
}
